<template>
  <div>
    <div class="main">
      <eagle-input
        name="CheckTypeName"
        label="证书类型"
        v-model="form.CertTypeName"
        required
        :islink="true"
        readonly
        @click="show=true"
        placeholder="请选择检查类型"
      ></eagle-input>
      <eagle-input
        name="calendar"
        v-model="form.CertCode"
        label="证书编号"
        placeholder="请输入证书编号"
        required
        isInputText
    />
    <eagle-input
        name="calendar"
        v-model="form.CertName"
        label="证书名称"
        placeholder="请输入证书编号"
        required
        isInputText
    />
    <eagle-input
        name="calendar"
        v-model="form.ChnName"
        label="所属员工"
        placeholder="请输入所属员工"
        required
        isInputText
    />
    <eagle-input
        name="calendar"
        v-model="form.ManagerName"
        label="证书管理员"
        placeholder="请输入证书管理员"
        required
        isInputText
    />
    <eagle-input
        name="calendar"
        v-model="form.CertOrg"
        label="发证机构"
        placeholder="请输入发证机构"
        isInputText
    />
      <eagle-date
        label="发证日期"
        required
        :date.sync="form.PublishDate"
      ></eagle-date>
      <eagle-input name="checkbox" label="是否需要复检">
        <template #input>
          <van-checkbox v-model="form.IsNeedReview" shape="square" @change="checkChange" />
        </template>
      </eagle-input>
      <eagle-date
        label="最近复审日期"
        required
        :date.sync="form.ReviewDate"
      />
      <eagle-date
        label="下次复审日期"
        required
        :date.sync="form.NextReviewDate"
      />
      <eagle-file-upload
        @CallBack="_fileUploadCb"
        :attachs="form.Attachs"
        :labelName="'证书附件'"
       
        labelWidth="100"
      />
      <eagle-input name="checkbox" label="公司保存原件">
        <template #input>
          <van-checkbox v-model="form.OriginalScript" shape="square" @change="checkChange2" />
        </template>
      </eagle-input>
      <div class="bottom-btns">
        <eagle-button type="primary" class="submit-btn" @click="save()">
          保存
        </eagle-button>
      </div>
    </div>
    <!--检查类型-->
    <check-type-dialog
      v-model="show"
      :data="form"
      @close="closeCheckType"
    ></check-type-dialog>
  </div>
</template>

<script>
import { mapGetters } from "vuex";
import UserChooseDialog from "@/views/components/user/userChooseDialog.vue";
import checkTypeDialog from "@/views/components/site/cert/checkTypeDialog.vue";
export default {
  components: {
    UserChooseDialog,
    checkTypeDialog,
  },
  data() {
    return {
      id: this.$route.query.id,
      userChooseDialogShow1: false,
      form: { Details: [] },
      show: false,
      plantypeShow: false,
      PlanTypeName:'',
      PlanType:'',
      CertType:''
    };
  },

  computed: {
    ...mapGetters(["baseUrl", "CompanyCode", "roles"]),
  },

  created() {
    if (this.$route.query.id != null) {
      this.id = this.$route.query.id
    }
    if (this.id == 0) {
      var d = new Date();
      this.form.Year = d.getFullYear().toString();
      this.getInitEntity()
    }else{
      this.getDetail();
    }
    
  
  },
  methods: {
    _fileUploadCb(attachs) {
      this.form.Attachs = attachs;
    },
    getDetail() {
      let params = { id: this.id };
      this.$axios
        .get("/SiteCert/GetDetail", {
          params: params,
        })
        .then((res) => {
          this.form = res.data;
          this.CertType=res.data.CertType
          res.data.CertTypeList.forEach(element => {
            if(element.ID==this.CertType){
              this.form.CertTypeName=element.Name
            }

          });
        });
    },
    getInitEntity() {
      let params = { id: this.id };
      this.$axios
        .get("/SiteCert/InitEntity", {
          params: params,
        })
        .then((res) => {
          this.form = res.data;
        });
    },
    // 选择检查类型
    closeCheckType(type) {
      console.log(type)
      if (type) {
          this.form.CertTypeList.forEach((item) => {
            if (item.ID == type) {
              this.form.CertTypeName = item.Name;
            }
          });
        
        this.form.CertType = type;
      }
      this.show = false;
    },


    save() {
      if (!this.form.CertType) {
        return this.$toast("请选择证书类型");
      }

      if (!this.form.CertCode) {
        return this.$toast("请输入证书编号");
      }
      if (!this.form.CertName) {
        return this.$toast("请输入证书名称");
      }
      if (!this.form.ChnName) {
        return this.$toast("请输入所属员工");
      }
      if (!this.form.ManagerName) {
        return this.$toast("请输入证书管理员");
      }
      if (!this.form.PublishDate) {
        return this.$toast("请选择发证日期");
      }
      if (!this.form.CertCode) {
        return this.$toast("请输入证书编号");
      }
      if(this.id==0&&this.form.IsNeedReview){
        return this.$toast("请输入最近复审日期");
      }
      this.$axios
        .post("/SiteCert/Edit", this.form)
        .then((res) => {
          this.$toast.success("保存成功");
          setTimeout(() => {
            this.$router.goRefresh(-1);
          }, 300);
        });
    },
    checkChange(value){

      this.form.IsNeedReview = value;
      
    },
    checkChange2(value){
    this.form.OriginalScript = value;

}
  },
};
</script>

<style lang="less" >·
.wrapper {
  display: flex;
  align-items: center;
  justify-content: center;
  height: 100%;
}
.block {
  width: 120px;
  height: 120px;
  background-color: #fff;
}
</style>
<style lang="less" scoped>
.space-between {
  justify-content: space-between;
}
.bottom-btns {
  margin-top: 10px;
  padding: 0 10px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  .submit-btn {
    width: 100%;
    height: 35px;
    font-size: 14px;
  }
}
</style>
